package com.csweimei.rentmanagementtreasure.utils.permission;

import android.app.Activity;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.csweimei.rentmanagementtreasure.R;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;

import java.util.List;

/**
 * shiys
 * 权限工具类
 */
public class RxPermissionsUtils {

    private static boolean isCreateFile = false; // 是否创建文件 - 同时标记是否得到该权限
    private static boolean isFile = false; //是否存在文件权限

    public interface PermissionBack{
        void onGranted();
        void onDenied();
    }

    // 文件权限
    public static void fileAll(Activity activity, Object obj) {
        String[] s = new String[]{Permission.MANAGE_EXTERNAL_STORAGE};
        Log.e("RxPermissionsUtils",""+XXPermissions.isGranted(activity,s));
        if (XXPermissions.isGranted(activity,s)){
            isFile = true;
            //申请的权限全部允许
            results(obj);
        }else {
            View rootView = MyCustomPopupWindow.inflateView(activity, R.layout.dialog_permission);
            TextView msg = rootView.findViewById(R.id.permission_msg);
            msg.setText("文件管理权限使用说明：\n访问设备上的照片、媒体内容和文件信息用于进行读写等场景操作");
            TextView mTvHint = rootView.findViewById(R.id.tv_hint);
            mTvHint.setText("是否允许“"+activity.getString(R.string.app_name)+"”获取“文件管理权限”");
            MyCustomPopupWindow mPopupWindow = MyCustomPopupWindow.builder()
                    .contentView(rootView)
                    .customListener((contentView) -> {
                    })
                    .activity(activity)
                    .gravity(Gravity.CENTER)
                    .isWrapHeight(true)
                    .isWrapWidth(true)
                    .build();
            mPopupWindow.show(0.5f);
            rootView.findViewById(R.id.btn_cancel).setOnClickListener(v -> {
                mPopupWindow.dismiss();
            });
            rootView.findViewById(R.id.btn_confirm).setOnClickListener(v -> {
                XXPermissions.with(activity)
                        .permission(s)
                        .request(new OnPermissionCallback() {
                            @Override
                            public void onGranted(List<String> permissions, boolean allGranted) {
                                if (allGranted){
                                    isFile = true;
                                    //申请的权限全部允许
                                    results(obj);
                                }
                            }

                            @Override
                            public void onDenied(List<String> permissions, boolean doNotAskAgain) {
                                OnPermissionCallback.super.onDenied(permissions, doNotAskAgain);
                            }
                        });
                mPopupWindow.dismiss();
            });
        }


    }


    // 文件权限
    public static void file(Activity activity, Object obj) {
        String[] s = new String[]{Permission.WRITE_EXTERNAL_STORAGE,Permission.READ_MEDIA_AUDIO,
                Permission.READ_MEDIA_IMAGES,Permission.READ_MEDIA_VIDEO};
        Log.e("RxPermissionsUtils",""+XXPermissions.isGranted(activity,s));
        if (XXPermissions.isGranted(activity,s)){
            isFile = true;
            //申请的权限全部允许
            results(obj);
        }else {
            View rootView = MyCustomPopupWindow.inflateView(activity, R.layout.dialog_permission);
            TextView msg = rootView.findViewById(R.id.permission_msg);
            msg.setText("文件管理权限使用说明：\n访问设备上的照片、媒体内容和文件信息用于进行读写等场景操作");
            TextView mTvHint = rootView.findViewById(R.id.tv_hint);
            mTvHint.setText("是否允许“"+activity.getString(R.string.app_name)+"”获取“文件管理权限”");
            MyCustomPopupWindow mPopupWindow = MyCustomPopupWindow.builder()
                    .contentView(rootView)
                    .customListener((contentView) -> {
                    })
                    .activity(activity)
                    .gravity(Gravity.CENTER)
                    .isWrapHeight(true)
                    .isWrapWidth(true)
                    .build();
            mPopupWindow.show(0.5f);
            rootView.findViewById(R.id.btn_cancel).setOnClickListener(v -> {
                mPopupWindow.dismiss();
            });
            rootView.findViewById(R.id.btn_confirm).setOnClickListener(v -> {
                XXPermissions.with(activity)
                        .permission(s)
                        .request(new OnPermissionCallback() {
                            @Override
                            public void onGranted(List<String> permissions, boolean allGranted) {
                                if (allGranted){
                                    isFile = true;
                                    //申请的权限全部允许
                                    results(obj);
                                }
                            }

                            @Override
                            public void onDenied(List<String> permissions, boolean doNotAskAgain) {
                                OnPermissionCallback.super.onDenied(permissions, doNotAskAgain);
                            }
                        });
                mPopupWindow.dismiss();
            });
        }


    }

    // 录音权限
    public static void record(Activity activity, Object obj) {
        if (XXPermissions.isGranted(activity,new String[]{Permission.MANAGE_EXTERNAL_STORAGE,Permission.RECORD_AUDIO})){
            isFile = true;
            //申请的权限全部允许
            results(obj);
        }else {
            View rootView = MyCustomPopupWindow.inflateView(activity, R.layout.dialog_permission);
            TextView msg = rootView.findViewById(R.id.permission_msg);
            msg.setText("文件管理权限使用说明：\n访问设备上的照片、媒体内容和文件信息用于进行读写等场景操作；\n" +
                    "录音（麦克风）权限使用说明：\n访问设备上的录音（麦克风）信息用于录制声音相关信息进行相应处理等场景操作。");
            TextView mTvHint = rootView.findViewById(R.id.tv_hint);
            mTvHint.setText("是否允许“"+activity.getString(R.string.app_name)+"”获取“文件管理权限、录音（麦克风）权限”");
            MyCustomPopupWindow mPopupWindow = MyCustomPopupWindow.builder()
                    .contentView(rootView)
                    .customListener((contentView) -> {
                    })
                    .activity(activity)
                    .gravity(Gravity.CENTER)
                    .isWrapHeight(true)
                    .isWrapWidth(true)
                    .build();
            mPopupWindow.show(0.5f);
            rootView.findViewById(R.id.btn_cancel).setOnClickListener(v -> {
                mPopupWindow.dismiss();
            });
            rootView.findViewById(R.id.btn_confirm).setOnClickListener(v -> {
                XXPermissions.with(activity)
                        .permission(new String[]{Permission.MANAGE_EXTERNAL_STORAGE,Permission.RECORD_AUDIO})
                        .request((permissions1, all) -> {
                            if (all){
                                isFile = true;
                                //申请的权限全部允许
                                results(obj);
                            }
                        });
                mPopupWindow.dismiss();
            });
        }


    }

    // 相机权限
    public static void camera(Activity activity, Object obj){
        if (XXPermissions.isGranted(activity,new String[]{Permission.MANAGE_EXTERNAL_STORAGE,Permission.CAMERA})){
            isFile = true;
            //申请的权限全部允许
            results(obj);
        }else {
            View rootView = MyCustomPopupWindow.inflateView(activity, R.layout.dialog_permission);
            TextView msg = rootView.findViewById(R.id.permission_msg);
            msg.setText("文件管理权限使用说明：\n访问设备上的照片、媒体内容和文件信息用于进行读写等场景操作；\n" +
                    "相机（摄像头）权限使用说明：\n访问设备上的相机（摄像头）信息用于拍摄相关信息进行相应处理等场景操作。");
            TextView mTvHint = rootView.findViewById(R.id.tv_hint);
            mTvHint.setText("是否允许“"+activity.getString(R.string.app_name)+"”获取“文件管理权限、相机（摄像头）权限”");
            MyCustomPopupWindow mPopupWindow = MyCustomPopupWindow.builder()
                    .contentView(rootView)
                    .customListener((contentView) -> {
                    })
                    .activity(activity)
                    .gravity(Gravity.CENTER)
                    .isWrapHeight(true)
                    .isWrapWidth(true)
                    .build();
            mPopupWindow.show(0.5f);
            rootView.findViewById(R.id.btn_cancel).setOnClickListener(v -> {
                mPopupWindow.dismiss();
            });
            rootView.findViewById(R.id.btn_confirm).setOnClickListener(v -> {
                XXPermissions.with(activity)
                        .permission(new String[]{Permission.MANAGE_EXTERNAL_STORAGE,Permission.CAMERA})
                        .request((permissions1, all) -> {
                            if (all){
                                isFile = true;
                                //申请的权限全部允许
                                results(obj);
                            }
                        });
                mPopupWindow.dismiss();
            });
        }
    }


    public static void SysSet(Activity activity, Object obj){

        if (XXPermissions.isGranted(activity,new String[]{Permission.WRITE_SETTINGS})){
            isFile = true;
            //申请的权限全部允许
            results(obj);
        }else {
            View rootView = MyCustomPopupWindow.inflateView(activity, R.layout.dialog_permission);
            TextView msg = rootView.findViewById(R.id.permission_msg);
            msg.setText("系统设置权限使用说明：\n用于修改系统允许范围内的设置等场景操作。");
            TextView mTvHint = rootView.findViewById(R.id.tv_hint);
            mTvHint.setText("是否允许“"+activity.getString(R.string.app_name)+"”获取“系统设置权限”");
            MyCustomPopupWindow mPopupWindow = MyCustomPopupWindow.builder()
                    .contentView(rootView)
                    .customListener((contentView) -> {
                    })
                    .activity(activity)
                    .gravity(Gravity.CENTER)
                    .isWrapHeight(true)
                    .isWrapWidth(true)
                    .build();
            mPopupWindow.show(0.5f);
            rootView.findViewById(R.id.btn_cancel).setOnClickListener(v -> {
                mPopupWindow.dismiss();
            });
            rootView.findViewById(R.id.btn_confirm).setOnClickListener(v -> {
                XXPermissions.with(activity)
                        .permission(Permission.WRITE_SETTINGS)
                        .request((permissions, allGranted) -> {
                            if (allGranted){
                                results(obj);

                            }
                        });
                mPopupWindow.dismiss();
            });
        }


    }

    public static void phone(Activity activity, Object obj){
        if (XXPermissions.isGranted(activity,new String[]{Permission.CALL_PHONE})){
            isFile = true;
            //申请的权限全部允许
            results(obj);
        }else {
            View rootView = MyCustomPopupWindow.inflateView(activity, R.layout.dialog_permission);
            TextView msg = rootView.findViewById(R.id.permission_msg);
            msg.setText("电话权限使用说明：\n访问设备上的电话权限用于获取设备的通话等场景操作");
            TextView mTvHint = rootView.findViewById(R.id.tv_hint);
            mTvHint.setText("是否允许“"+activity.getString(R.string.app_name)+"”获取“电话权限”");
            MyCustomPopupWindow mPopupWindow = MyCustomPopupWindow.builder()
                    .contentView(rootView)
                    .customListener((contentView) -> {
                    })
                    .activity(activity)
                    .gravity(Gravity.CENTER)
                    .isWrapHeight(true)
                    .isWrapWidth(true)
                    .build();
            mPopupWindow.show(0.5f);
            rootView.findViewById(R.id.btn_cancel).setOnClickListener(v -> {
                mPopupWindow.dismiss();
            });
            rootView.findViewById(R.id.btn_confirm).setOnClickListener(v -> {
                XXPermissions.with(activity)
                        .permission(Permission.CALL_PHONE)
                        .request((permissions, allGranted) -> {
                            if (allGranted){
                                results(obj);

                            }
                        });
                mPopupWindow.dismiss();
            });
        }


    }


    // 只处理成功授权的结果
    private static void results(Object o) {
        if (o instanceof Class) {
            Class c = (Class) o;
        } else if (o instanceof Accept) {
            Accept accept = (Accept) o;
            accept.accept();
        }
    }

    public interface Accept {
        void accept(); // 只需要返回成功的
    }
}
